package org.jeecg.modules.sggg.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.sggg.entity.SgFollowUp;
import org.jeecg.modules.sggg.entity.SgOralcavity;
import org.jeecg.modules.sggg.entity.SgTumor;
import org.jeecg.modules.sggg.service.ISgTumorService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.sggg.service.ISgTumorService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 肿瘤筛查
 * @Author: jeecg-boot
 * @Date:   2023-05-24
 * @Version: V1.0
 */
@Api(tags="肿瘤筛查")
@RestController
@RequestMapping("/sgtumor/sgTumor")
@Slf4j
public class SgTumorController extends JeecgController<SgTumor, ISgTumorService> {
	@Autowired
	private ISgTumorService sgTumorService;

	@Autowired
	private org.jeecg.modules.sggg.mapper.SgTumorMapper sgTumorMapper;
	
	/**
	 * 分页列表查询
	 *
	 * @param sgTumor
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "肿瘤筛查-分页列表查询")
	@ApiOperation(value="肿瘤筛查-分页列表查询", notes="肿瘤筛查-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<SgTumor>> queryPageList(SgTumor sgTumor,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SgTumor> queryWrapper = QueryGenerator.initQueryWrapper(sgTumor, req.getParameterMap());
		Page<SgTumor> page = new Page<SgTumor>(pageNo, pageSize);
		IPage<SgTumor> pageList = sgTumorService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param sgTumor
	 * @return
	 */
	@AutoLog(value = "肿瘤筛查-添加")
	@ApiOperation(value="肿瘤筛查-添加", notes="肿瘤筛查-添加")
	//@RequiresPermissions("sgtumor:sg_tumor:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody SgTumor sgTumor) {
		sgTumorService.save(sgTumor);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param sgTumor
	 * @return
	 */
	@AutoLog(value = "肿瘤筛查-编辑")
	@ApiOperation(value="肿瘤筛查-编辑", notes="肿瘤筛查-编辑")
	//@RequiresPermissions("sgtumor:sg_tumor:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody SgTumor sgTumor) {
		sgTumorService.updateById(sgTumor);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "肿瘤筛查-通过id删除")
	@ApiOperation(value="肿瘤筛查-通过id删除", notes="肿瘤筛查-通过id删除")
	//@RequiresPermissions("sgtumor:sg_tumor:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		sgTumorService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "肿瘤筛查-批量删除")
	@ApiOperation(value="肿瘤筛查-批量删除", notes="肿瘤筛查-批量删除")
	//@RequiresPermissions("sgtumor:sg_tumor:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sgTumorService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "肿瘤筛查-通过id查询")
	@ApiOperation(value="肿瘤筛查-通过id查询", notes="肿瘤筛查-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<SgTumor> queryById(@RequestParam(name="id",required=true) String id) {
//		id = "1668201203036495873";
		SgTumor sgTumor = sgTumorService.getById(id);
		if(sgTumor==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(sgTumor);
	}

	 @ApiOperation(value = "随访表单-查询某个用户所有的随访表单列表", notes = "随访表单-查询某个用户所有的随访表单列表")
	 @GetMapping(value = "/queryListById")
	 public Result<List<SgTumor>> queryForm(@RequestParam(name = "id") String id) {
		 QueryWrapper<SgTumor> server = new QueryWrapper<>();
		 server.eq("member_id", id).orderByDesc("create_time").select("create_time", "id");
		 List<SgTumor> data = sgTumorService.list(server);
		 return Result.OK(data);
	 }

	 /**
	  * 通过memberId查询
	  *
	  * @param
	  * @return
	  */
	 //@AutoLog(value = "肿瘤筛查-通过id查询")
	 @ApiOperation(value="肿瘤筛查-通过memberId查询", notes="肿瘤筛查-通过memberId查询")
	 @GetMapping(value = "/queryByMember")
	 public Result<List<SgTumor>> queryByMember(String memberId) {
		 List<SgTumor> List = sgTumorService.list(new QueryWrapper<SgTumor>().eq("member_id", memberId).orderByDesc("create_time"));
		 if(List==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(List);
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param sgTumor
    */
    //@RequiresPermissions("sgtumor:sg_tumor:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SgTumor sgTumor) {
        return super.exportXls(request, sgTumor, SgTumor.class, "肿瘤筛查");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("sgtumor:sg_tumor:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SgTumor.class);
    }

}
